package com.app.manage.components;

import cn.hutool.core.date.DateUtil;
import com.app.manage.moudles.system.constants.LogTypeEnums;
import com.app.manage.moudles.system.dao.TLogDao;
import com.app.manage.moudles.system.entity.TLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;

@Component
public class CommonComponent {

    @Resource
    private TLogDao logDao;

    /**
     * 获取到上传的文件路径
     * @param date
     * @return
     */
    public String findAppUploadPath(Date date){
      return  System.getProperty("user.dir")+ File.separator+"push"+File.separator+ DateUtil.format(date,"yyyyMMdd")+File.separator;
    }

    /**
     * 获取到应用日志存储在本地的路径
     * @param date
     * @return
     */
    public String findAppLogPathAtLocal(Date date){
        return  System.getProperty("user.dir")+ File.separator+"appLogPathAtLocal"+File.separator+DateUtil.format(date,"yyyyMMddHHmmss")+File.separator;
    }


    /**
     * 添加一个任务日志
     * @param logName
     * @param remark
     * @param taskId
     * @return
     */
    public String addTaskLog(String logName,String remark,Integer taskId){
        TLog log = new TLog();
        log.setLogName(logName);
        log.setStartTime(new Date());
        log.setRemark(remark);
        log.setLogType(LogTypeEnums.PUSH_TASK.getType());
        log.setLogTypeName(LogTypeEnums.PUSH_TASK.getName());
        log.setRelId(taskId);
        logDao.insert(log);
        return "success";
    }

    public String removeTaskLog(Integer taskId){
        logDao.delete(new LambdaQueryWrapper<TLog>()
                .eq(TLog::getLogType,LogTypeEnums.PUSH_TASK.getType())
                .eq(TLog::getRelId,taskId)
        );
        return null;
    }
}
